local assets =
{
    Asset("ANIM", "anim/sw_infernal_stone.zip"),
}

local function onfinish(inst, worker)
    local entrance = inst.parent
    if entrance then
        entrance.components.sw_cordon.cordon = false
        SendModRPCToShard(SHARD_MOD_RPC.SHADOWWOVEN.Cordon, entrance.components.worldmigrator.linkedWorld, entrance.components.worldmigrator.id, false)
        if TheWorld and TheWorld.components.sw_sealsinkholemanager then
            TheWorld.components.sw_sealsinkholemanager:RefreshUnbalanceState()
        end
    end
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    -- MakeObstaclePhysics(inst, 0.2)

    inst.AnimState:SetBank("sw_infernal_stone")
    inst.AnimState:SetBuild("sw_infernal_stone")
    inst.AnimState:PlayAnimation("idle")

    inst.MiniMapEntity:SetIcon("sw_infernal_stone.tex")

    inst:AddTag("structure")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.MINE)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onfinish)

    return inst
end

local function fn_item()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("sw_infernal_stone")
    inst.AnimState:SetBuild("sw_infernal_stone")
    inst.AnimState:PlayAnimation("item")

    inst.pickupsound = "rock"

    inst:AddTag("sw_cordon")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetSinks(true)

    inst:AddComponent("sw_cordon")

    MakeHauntableLaunchAndSmash(inst)

    return inst
end

return Prefab("sw_infernal_stone", fn, assets),
    Prefab("sw_infernal_stone_item", fn_item, assets)
